package com.yq.servlet;


import com.yq.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Enumeration;

//设计一个form表单，尝试使用反射获取客户端提交过来的请求参数，并且封装到对象中

@WebServlet("/ss2")
public class ServletDemmo2 extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数
        String username = req.getParameter("username");
        String age = req.getParameter("age");
        String score = req.getParameter("score");

        //封装
        Class<?> aClass = null;
        try {
            aClass = Class.forName("com.yq.bean.Student");
            Student stu = (Student) aClass.newInstance();
            Method setusername = aClass.getMethod("setUsername",String.class);
            Method setAge = aClass.getMethod("setAge",int.class);
            Method setScore = aClass.getMethod("setScore",int.class);
            setusername.invoke(stu, username);
            setAge.invoke(stu,Integer.parseInt(age));
            setScore.invoke(stu,Integer.parseInt(score));
            System.out.println(stu);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
